package com.uls.web.controller.hdh;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.hdh.domain.HdhPollResults;
import com.uls.hdh.domain.HdhPolls;
import com.uls.hdh.service.IHdhPollResultsService;
import com.uls.hdh.service.IHdhPollsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.hdh.domain.HdhOptions;
import com.uls.hdh.service.IHdhOptionsService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 选项Controller
 * 
 * @author hdh
 * @date 2024-11-09
 */
@RestController
@RequestMapping("/hdh/options")
public class HdhOptionsController extends BaseController
{
    @Autowired
    private IHdhOptionsService hdhOptionsService;

    @Autowired
    private IHdhPollsService hdhPollsService;
    @Autowired
    private IHdhPollResultsService hdhPollResultsService;
    /**
     * 查询选项列表
     */
    @PreAuthorize("@ss.hasPermi('hdh:options:list')")
    @GetMapping("/list")
    public TableDataInfo list(HdhOptions hdhOptions)
    {
        startPage();
        List<HdhOptions> list = hdhOptionsService.selectHdhOptionsList(hdhOptions);
        for (HdhOptions hdhOptions1:list){
            Long pollId=hdhOptions1.getPollId();
            HdhPolls hdhPolls=hdhPollsService.selectHdhPollsByPollId(pollId);
            if(hdhPolls!=null){
                hdhOptions1.setTitle(hdhPolls.getTitle());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出选项列表
     */
    @PreAuthorize("@ss.hasPermi('hdh:options:export')")
    @Log(title = "选项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HdhOptions hdhOptions)
    {
        List<HdhOptions> list = hdhOptionsService.selectHdhOptionsList(hdhOptions);
        ExcelUtil<HdhOptions> util = new ExcelUtil<HdhOptions>(HdhOptions.class);
        util.exportExcel(response, list, "选项数据");
    }

    /**
     * 获取选项详细信息
     */
    @PreAuthorize("@ss.hasPermi('hdh:options:query')")
    @GetMapping(value = "/{optionId}")
    public AjaxResult getInfo(@PathVariable("optionId") Long optionId)
    {
        return success(hdhOptionsService.selectHdhOptionsByOptionId(optionId));
    }

    /**
     * 新增选项
     */
    @PreAuthorize("@ss.hasPermi('hdh:options:add')")
    @Log(title = "选项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HdhOptions hdhOptions)
    {
        hdhOptions.setCreatedBy(this.getUsername());
        hdhOptions.setCreatedTime(new Date());
        hdhOptionsService.insertHdhOptions(hdhOptions);
        HdhPollResults hdhPollResults=new HdhPollResults();
        hdhPollResults.setPollId(hdhOptions.getPollId());
        hdhPollResults.setOptionId(hdhOptions.getOptionId());
        hdhPollResults.setTotalVotes(0l);
        hdhPollResults.setWeightedVotes(0l);
        hdhPollResults.setCreatedBy(this.getUsername());
        hdhPollResults.setCreatedTime(new Date());
        hdhPollResultsService.insertHdhPollResults(hdhPollResults);
        return toAjax(true);
    }

    /**
     * 修改选项
     */
    @PreAuthorize("@ss.hasPermi('hdh:options:edit')")
    @Log(title = "选项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HdhOptions hdhOptions)
    {
        hdhOptions.setUpdatedBy(this.getUsername());
        hdhOptions.setUpdatedTime(new Date());
        return toAjax(hdhOptionsService.updateHdhOptions(hdhOptions));
    }

    /**
     * 删除选项
     */
    @PreAuthorize("@ss.hasPermi('hdh:options:remove')")
    @Log(title = "选项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{optionIds}")
    public AjaxResult remove(@PathVariable Long[] optionIds)
    {
        return toAjax(hdhOptionsService.deleteHdhOptionsByOptionIds(optionIds));
    }

    @GetMapping("/getAllOptions")
    public AjaxResult getAllOptions()
    {
        List<HdhOptions> list = hdhOptionsService.selectHdhOptionsList(null);
        return success(list);
    }


}
